﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics.Dynamics;

namespace SampleV1
{
    public abstract class Character: View
    {
        public Texture2D Texture { get; set; }

        public string ID { get; set; }

        public Point SheetSize { get; set; }

        public int MilliSecondsPerFrame { get; set; }

        public Vector2 Speed { get; set; }

        public Point currentFrame;

        public int timeSinceLastFrame;


        public Vector2 Position { get; set; }
        public Point FrameSize { get; set; }
        public Point Offset { get; set; }
        public Vector2 Origin { get; set; }
        public float Scale { get; set; }

        public Body Body { get; set; }
        public int Health { get; set; }



        public override void Update(GameTime gameTime, Rectangle windowClient)
        {
            //control framerate
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > MilliSecondsPerFrame)
            {
                timeSinceLastFrame = 0;
                currentFrame.X++;
                if (currentFrame.X > SheetSize.X)
                {
                    currentFrame.X = 0;
                    currentFrame.Y++;
                    if (currentFrame.Y > SheetSize.Y)
                    {
                        currentFrame.Y = 0;
                    }//end if
                }//end if
            }//end if
        }//end update

        public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            if (IsVisible&&Texture!=null)
                spriteBatch.Draw(Texture, Position, new Rectangle(currentFrame.X * FrameSize.X, currentFrame.Y * FrameSize.Y, FrameSize.X, FrameSize.Y), Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
        }//end draw


        public override void Update(GameTime gameTime)
        {
            //control framerate
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > MilliSecondsPerFrame)
            {
                timeSinceLastFrame = 0;
                currentFrame.X++;
                if (currentFrame.X > SheetSize.X)
                {
                    currentFrame.X = 0;
                    currentFrame.Y++;
                    if (currentFrame.Y > SheetSize.Y)
                    {
                        currentFrame.Y = 0;
                    }//end if
                }//end if
            }//end if
        }

        public Character()
        {
        }

        public Character(Texture2D Texture)
        {
            this.Texture = Texture;
            Scale = 1.0f;
        }//end ctor

        public Character(Texture2D Texture, Vector2 Origin)
        {
            this.Texture = Texture;
            this.Origin = Origin;
            this.Position = Origin - new Vector2(FrameSize.X/2, FrameSize.Y/2);
            Scale = 1.0f;
        }//end ctor
    }
}
